using BuildStatusIndicator.Controller.EventBroker;
using BuildStatusIndicator.Controller.Events;
using Microsoft.Practices.Unity;
using Microsoft.TeamFoundation.Build.Client;

namespace BuildStatusIndicator.DelCom
{
    public class DelComBuildIndicator : IListener<BuildSucceededEvent>, IListener<BuildFailedEvent>,
                                        IListener<BuildPartiallySucceededEvent>, IListener<BuildStoppedEvent>,
                                        IListener<BuildInProgressEvent>, IListener<BuildNotStartedEvent>,
                                        IListener<BuildNotificationEndingEvent>,
                                        IListener<BuildNotificationStartingEvent>
    {
        private readonly IUnityContainer _container;

        public DelComBuildIndicator(IUnityContainer containter, IBuildStatusEventPublisher eventPublisher)
        {
            _container = containter;
            eventPublisher.AddListener(this);
        }

        #region IListener<BuildFailedEvent> Members

        public void Handle(BuildFailedEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                if (subject.BuildDetail.TestStatus == BuildPhaseStatus.Failed)
                    delCom.FlashRed();
                else
                    delCom.TurnOnRed();
            }
        }

        #endregion

        #region IListener<BuildInProgressEvent> Members

        public void Handle(BuildInProgressEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                delCom.TurnOnYellowOrBlue();
            }
        }

        #endregion

        #region IListener<BuildNotificationEndingEvent> Members

        public void Handle(BuildNotificationEndingEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                delCom.TurnOff();
            }
        }

        #endregion

        #region IListener<BuildNotificationStartingEvent> Members

        public void Handle(BuildNotificationStartingEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                delCom.TurnOnGreen();
            }
        }

        #endregion

        #region IListener<BuildNotStartedEvent> Members

        public void Handle(BuildNotStartedEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                delCom.TurnOnYellowOrBlue();
            }
        }

        #endregion

        #region IListener<BuildPartiallySucceededEvent> Members

        public void Handle(BuildPartiallySucceededEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                delCom.TurnOnRed();
            }
        }

        #endregion

        #region IListener<BuildStoppedEvent> Members

        public void Handle(BuildStoppedEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                delCom.TurnOnRed();
            }
        }

        #endregion

        #region IListener<BuildSucceededEvent> Members

        public void Handle(BuildSucceededEvent subject)
        {
            using (IDelComVisualSignalIndicator delCom = GetDelComIndicator())
            {
                delCom.TurnOnGreen();
            }
        }

        #endregion

        private IDelComVisualSignalIndicator GetDelComIndicator()
        {
            return _container.Resolve<IDelComVisualSignalIndicator>();
        }
    }
}